package org.bw.bookstore.book.service.impl;

import org.bw.bookstore.book.mapper.EnterRecordMapper;
import org.bw.bookstore.book.mapper.MemberMapper;
import org.bw.bookstore.book.mapper.OutRecordMapper;
import org.bw.bookstore.book.service.UserFlowRecordService;
import org.bw.bookstore.common.entity.TbEnterRecord;
import org.bw.bookstore.common.entity.TbOutRecord;
import org.bw.bookstore.common.entity.TbUserInfo;
import org.bw.bookstore.common.utils.ResponseResult;
import org.bw.bookstore.common.utils.StringTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author GuoShuai
 * @Date 2020-10-22 下午 03:02
 * @Version 1.0
 **/
@Service
public class UserFlowRecordServiceimpl implements UserFlowRecordService {

    @Autowired
    EnterRecordMapper enterRecordMapper;
    @Autowired
    OutRecordMapper outRecordMapper;
    @Autowired
    MemberMapper memberMapper;

    public ResponseResult progressRecord(Integer userInfoId) {
        ResponseResult result = ResponseResult.SUCCESS();
        //查询当前用户是否存在
        TbUserInfo admin = memberMapper.selectById(userInfoId);
        if (admin == null){
            return result.setFail("用户不存在");
        }
        TbEnterRecord tbEnterRecord = new TbEnterRecord();


        //保存用户进店记录
        //添加关联用户唯一标识
        tbEnterRecord.setUserInfoCode(admin.getCode());
        //获得当前时间
        Date date = new Date();
        tbEnterRecord.setCreateTime(date);
        //添加唯一标识
        String randomString = StringTools.getRandomString(32);
        tbEnterRecord.setCode(randomString);
        enterRecordMapper.insert(tbEnterRecord);
        return result;
    }

    @Override
    public ResponseResult outIntoRecord(Integer userInfoId) {
        ResponseResult result = ResponseResult.SUCCESS();
        //查询当前用户是否存在
        TbUserInfo admin = memberMapper.selectById(userInfoId);
        if (admin == null){
            return result.setFail("用户不存在");
        }
        //保存用户进店记录

        TbOutRecord tbOutRecord = new TbOutRecord();
        //添加关联用户唯一标识
        tbOutRecord.setUserInfoCode(admin.getCode());
        //添加唯一标识
        String randomString = StringTools.getRandomString(32);
        tbOutRecord.setCode(randomString);
        //获得当前时间
        Date date = new Date();
        admin.setCode(randomString);
        tbOutRecord .setCreateTime(date);
        outRecordMapper.insert(tbOutRecord);
        return result;
    }

    @Override
    public ResponseResult flowRecord(Integer userInfoId) {
        ResponseResult result = ResponseResult.SUCCESS();
        Map<String , Object> resultMap = new HashMap<>();
        //查询当前用户是否存在
        TbUserInfo admin = memberMapper.selectById(userInfoId);
        if (admin == null){
            return result.setFail("用户不存在");
        }

        //查询当前用户的流动记录
        String idCard = admin.getIdCard();

        List<TbEnterRecord> tbEnterRecords= enterRecordMapper.tbEnterselectFlowRecord(admin.getIdCard());

        List<TbOutRecord> tbOutRecords= outRecordMapper.tbOutFlowVoRecord(admin.getIdCard());
        resultMap.put("username",admin.getUserName());
        resultMap.put("tbEnterRecords",tbEnterRecords);
        resultMap.put("tbOutRecords",tbOutRecords);

        //对应唯一标识查到两条进出门数据

        if(tbEnterRecords==null|| tbOutRecords==null){
             resultMap.put("message","用户数据异常");
        }

        result.setResult(resultMap);
//        Map<String , Object> resultMap = new HashMap<>();
//        //查询进门记录
//        List<TbEnterRecord> enterRecords=enterRecordMapper.selectList(new QueryWrapper());
//        resultMap.put("enterRecords",enterRecords);
//        //查询出门记录
//        List<TbOutRecord> outRecords=outRecordMapper.selectList(new QueryWrapper());
//        resultMap.put("outRecords",outRecords);
//        result.setResult(resultMap);
        return result;

}


}
